package com.pinyoumei.app.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.akexorcist.roundcornerprogressbar.RoundCornerProgressBar;
import com.pinyoumei.app.R;
import com.pinyoumei.app.utils.Utils;


/**
 * APP版本升级弹框
 */
public class APPUpdateDialog extends Dialog implements View.OnClickListener{
    private Context context;
    private RoundCornerProgressBar progress;//带圆角的进度条
    private RelativeLayout relative_app_update;//版本升级
    private FrameLayout framalayout;
    private TextView text_code_version;
    private TextView tv_description;
    private String description;

    public APPUpdateDialog(Context context,int themeResId){
        super(context,themeResId);
        this.context = context;
    }
    public APPUpdateDialog(Context context,boolean cancelable,OnCancelListener cancelListener){
        super(context,cancelable,cancelListener);
    }
    //自定义构造方法
    public APPUpdateDialog(Context context,String description){
        super(context, R.style.dialogStyle);//这个决定dialog的样式
        this.context = context;
        this.description = description;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        Window window = getWindow();//获取一个窗口
        window.setGravity(Gravity.CENTER);//设置窗口的位置
        //window.setWindowAnimations(R.style.bottom_menu_animation);//设置窗口进入时的动画特效
        WindowManager.LayoutParams layoutParams = window.getAttributes();//获取窗口的属性赋值给layoutParams
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);//设置窗口的属性
//        setCanceledOnTouchOutside(true);
        setContentView(R.layout.dialog_layout_appupdate);//放布局文件
        relative_app_update = findViewById(R.id.relative_app_update);
        relative_app_update.setOnClickListener(this);
        //relative_app_update.setVisibility(View.VISIBLE);
        progress = findViewById(R.id.progress);
        framalayout = findViewById(R.id.framalayout);
        text_code_version = findViewById(R.id.text_code_version);
        text_code_version.setText(Utils.getVerName(context));//升级框的字数

        tv_description = findViewById(R.id.tv_description);
        tv_description.setText(description);//升级框的描述
        //progress.setVisibility(View.GONE);
        /*//遍历控件id,添加点击事件
        for (int id : listenedItems){
            findViewById(id).setOnClickListener(this);
        }*/
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.relative_app_update:
                listener.onClick(v);
                break;
        }
    }
    @Override
    public void show(){
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity = Gravity.CENTER;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);
    }
    //自定义一个监听的接口回调
    private OnClickListener listener;
    public interface OnClickListener{
        void onClick(View view);
    }
    public void setOnClickListener(OnClickListener listener){
        this.listener = listener;
    }
    public void showProgress(){
        relative_app_update.setVisibility(View.GONE);
        framalayout.setVisibility(View.VISIBLE);
    }
    //设置进度条的值
    public void setProgress(float value){
        progress.setProgress(value);
    }
}
